home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
graphic
/
fghedge.zip
/
HEDGE.C
< prev
next >
Wrap
Text File
|
1993-10-11
|
23KB
|
876 lines
/****************************************************************************\
* *
* Hedge.c *
* *
* Copyright 1993 Diana Gruber. All rights reserved. *
* *
* This program is copyrighted freeware, distributed with source code. *
* Code is provided "as is" without any warranties. You may use portions *
* of this code in your own programs as long as you do not use more than *
* 50% of the code in any one program. *
* *
* This code is provided for instructional purposes. It requires Fastgraph *
* or Fastgraph/Light to link. *
* *
* For more information, including the compilation and linking commands for *
* all supported compilers, please see the HEDGE.DOC file. *
* *
\****************************************************************************/
#include "defs.h"
#define NMAZES 7
/****************************************************************************\
* *
* main -- all functions are in alphabetical order except this one *
* *
\****************************************************************************/
void main()
{
register int i,j;
int n;
char *maze_name[] =
{
"maze001.lev",
"maze002.lev",
"maze003.lev",
"maze004.lev",
"maze005.lev",
"maze006.lev",
"maze007.lev"
};
/* open the attribute file and read the attributes */
if ((tstream = fopen("hedge.att","rb")) == NULL)
{
sprintf(abort_string,"HEDGE.ATT not found.");
abort_game();
}
fread(attributes,sizeof(char),240,tstream);
fclose(tstream);
/* initialize SVGA graphics */
init_graphics();
fg_tcmask(1);
intro_screen();
/* load the dub diner file on the hidden page */
fg_setpage(HIDDEN);
fg_erase();
fg_move(80,60);
fg_showpcx("DUBDINER.PCX",2);
/* copy it to the logical page (EMS or XMS) */
fg_copypage(HIDDEN,SPARE);
/* display the maze file on the hidden page */
fg_move(0,0);
fg_showpcx("HEDGE.PCX",1);
fg_setpage(VISUAL);
fg_setcolor(0);
center_string("Please wait...",400,300);
fg_setcolor(7);
center_string("Press any key to continue.",400,300);
fg_waitkey();
for (n = 0; n < NMAZES; n++)
{
fg_setrgb(7,63,63,63);
fg_setrgb(5,63,63,63);
fg_setrgb(2,63,63,63);
fg_mousevis(OFF);
fg_copypage(SPARE,VISUAL);
load_maze(maze_name[n]);
for (i = 0; i <50; i++)
{
for (j = 0; j <37; j++)
visited[i][j] = 0;
}
for (i = 0; i < 2000; i++)
path[i] = -1;
path_index = 1;
flushkey();
if (n == 0) help_window();
fg_mousevis(ON);
run_maze();
}
quit_graphics();
}
/****************************************************************************\
* *
* calc_truepath -- recursive algorithm to solve maze *
* *
\****************************************************************************/
int calc_truepath(int i,int j)
{
int tile,nexttile;
int attr,nextattr;
int x1,x2,y1,y2;
int found;
int index;
int stall_time;
int left,right,top,bottom;
int horizontal,vertical;
int nextcross;
stall_time = clockspeed/4;
visited[i][j] = TRUE;
index = path_index;
while(1)
{
fg_stall(stall_time);
tile = layout[i][j];
attr = attributes[tile];
found = FALSE;
x1 = i*16 + 6;
x2 = x1 + 4;
y1 = j*16 + 6;
y2 = y1 + 4;
left = attr&LEFT;
right = attr&RIGHT;
top = attr&TOP;
bottom = attr&BOTTOM;
vertical = attr&VERTICAL;
horizontal = attr&HORIZONTAL;
if (horizontal || vertical)
{
if (path[index-1] == LEFT || path[index-1] == RIGHT)
{
top = FALSE; bottom = FALSE;
}
else if (path[index-1] == TOP || path[index-1] == BOTTOM)
{
left = FALSE; right = FALSE;
}
}
/* check right */
if (j < MAXCOLS-1 && !found && !(path[index-1] == LEFT))
{
nexttile = layout[i+1][j];
nextattr = attributes[nexttile];
nextcross = ((nextattr&VERTICAL) + (nextattr&HORIZONTAL));
if (!visited[i+1][j] || (nextcross && (visited[i+1][j] != HORIZONTAL)))
{
if (right && nextattr&LEFT)
{
found = TRUE;
path[index++] = RIGHT;
i++;
visited[i][j] = HORIZONTAL;
x2 = i*16 + 10;
if (vertical)
x1 += 9;
if (nextattr&VERTICAL)
x2 -= 9;
}
}
}
/* check UP */
if (j > 0 && !found && !(path[index-1] == BOTTOM))
{
nexttile = layout[i][j-1];
nextattr = attributes[nexttile];
nextcross = ((nextattr&VERTICAL) + (nextattr&HORIZONTAL));
if (!visited[i][j-1] || (nextcross && (visited[i][j-1] != VERTICAL)))
{
if (top && nextattr&BOTTOM)
{
found = TRUE;
path[index++] = TOP;
j--;
visited[i][j] = VERTICAL;
y1 = j*16 + 6;
if (nextattr&HORIZONTAL)
y1 += 9;
if (horizontal)
y2 -= 9;
}
}
}
/* check down */
if (j < MAXROWS-1 && !found && !(path[index-1] == TOP))
{
nexttile = layout[i][j+1];
nextattr = attributes[nexttile];
nextcross = ((nextattr&VERTICAL) + (nextattr&HORIZONTAL));
if (!visited[i][j+1] || (nextcross && (visited[i][j+1] != VERTICAL)))
{
if (bottom && nextattr&TOP)
{
found = TRUE;
path[index++] = BOTTOM;
j++;
visited[i][j] = VERTICAL;
y2 = j*16 + 10;
if (horizontal)
y1 += 9;
if (nextattr&HORIZONTAL)
y2 -= 9;
}
}
}
/* check left */
if (i > 0 && !found && !(path[index-1] == RIGHT))
{
nexttile = layout[i-1][j];
nextattr = attributes[nexttile];
nextcross = ((nextattr&VERTICAL) + (nextattr&HORIZONTAL));
if (!visited[i-1][j] || (nextcross && (visited[i-1][j] != HORIZONTAL)))
{
if (left && nextattr&RIGHT)
{
found = TRUE;
path[index++] = LEFT;
i--;
visited[i][j] = HORIZONTAL;
x1 = i*16 + 6;
if (nextattr&VERTICAL)
x1 += 9;
if (vertical)
x2 -= 9;
}
}
}
/* adjacent tile found */
if (found)
{
fg_setcolor(0);
fg_rect(x1,x2,y1,y2);
if (is_a_vertex(nexttile))
{
path_index = index;
/* You are at a vertex, do a recursive function call */